package cn.itcast.service.impl;

import cn.itcast.dao.cargo.InvoiceDao;
import cn.itcast.domain.cargo.Invoice;
import cn.itcast.domain.cargo.InvoiceExample;
import cn.itcast.dao.cargo.ShippingDao;
import cn.itcast.domain.cargo.Invoice;
import cn.itcast.domain.cargo.InvoiceExample;
import cn.itcast.domain.cargo.Shipping;
import cn.itcast.service.InvoiceService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class InvoiceServiceImpl implements InvoiceService {
    @Autowired
    private InvoiceDao invoiceDao;
    @Autowired
    private ShippingDao shippingDao;
    @Override
    public PageInfo findPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        InvoiceExample invoiceExample = new InvoiceExample();

        List<Invoice> list = invoiceDao.selectByExample(invoiceExample);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public Invoice selectById(String id) {
        Invoice invoice = invoiceDao.selectByPrimaryKey(id);
        return invoice;
    }

    @Override
    public void deleteByid(String id) {
        invoiceDao.deleteByPrimaryKey(id);
    }

    @Override
    public void addInvoice(Invoice invoice) {
        invoiceDao.insertSelective(invoice);
        Shipping shipping = new Shipping();
        shipping.setState(1);
        shipping.setShippingOrderId(invoice.getInvoiceId());
        shippingDao.updateByPrimaryKeySelective(shipping);

    }

    @Override
    public void updateInvoice(Invoice invoice) {
        invoiceDao.updateByPrimaryKeySelective(invoice);
    }
}
